package com.bRoom.controller.back;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.pojo.Admin;
import com.bRoom.pojo.User;
import com.bRoom.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("back/admin")
public class IAdminManageController {

    @Autowired
    private IAdminService iAdminService;

    @RequestMapping("add_admin.do")
    @ResponseBody
    public ServerResponse<Admin> addAdmin(Admin admin, HttpSession session){

        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){
            return iAdminService.addAdmin(admin);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }

    @RequestMapping("update_admin.do")
    @ResponseBody
    public ServerResponse<Admin> updateAdmin(Admin admin,HttpSession session){

        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){
            return iAdminService.updateAdmin(admin);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }

}
